{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0fa4d687-14f1-44ed-9306-dd1bd7cdbe2e",
   "metadata": {},
   "source": [
    "# ImageJ, Python, and itkwidgets\n",
    "\n",
    "### Try this notebook in Binder or SageMaker!\n",
    "\n",
    "[![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/InsightSoftwareConsortium/itkwidgets/HEAD?labpath=examples%2Fintegrations%2FPyImageJ%2FImageJImgLib2.ipynb)\n",
    "[![Open In SageMaker Studio Lab](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github.com/InsightSoftwareConsortium/itkwidgets/blob/main/examples/integrations/PyImageJ/ImageJImgLib2.ipynb)\n",
    "\n",
    "This example demonstrates how to use ImageJ from CPython and how it can be used with itkwidgets.\n",
    "\n",
    "To run this example, use the conda cross-platform package manager and install the pyimagej package from conda-forge.\n",
    "```\n",
    "conda install -c conda-forge pyimagej itk\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1f72f7cd-87b1-42b2-bf17-c5559a0c7c7a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Install dependencies for this example\n",
    "import sys\n",
    "\n",
    "!conda install --yes --prefix {sys.prefix} -c conda-forge pyimagej\n",
    "!{sys.executable} -m pip install -q \"itkwidgets[all]>=1.0a23\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6d179820-c655-46fe-ad21-da1166907547",
   "metadata": {},
   "outputs": [],
   "source": [
    "from urllib.request import urlretrieve\n",
    "import os\n",
    "\n",
    "import itk\n",
    "import imagej\n",
    "import numpy as np\n",
    "\n",
    "from itkwidgets import view"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9bfbf59b-59d4-46eb-84d0-344c54177391",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Initialize imagej\n",
    "ij = imagej.init()\n",
    "print(ij.getVersion())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dde35ec8-e294-412a-b809-3f569b6d087c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Download data\n",
    "file_name = 'General_EduRes_Heart_BloodVessels_0.jpg'\n",
    "if not os.path.exists(file_name):\n",
    "    url = 'https://data.kitware.com/api/v1/file/5afe74408d777f15ebe1d701/download'\n",
    "    urlretrieve(url, file_name)\n",
    "image = itk.imread(file_name, itk.ctype('float'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6c58b5a6-2d3d-4511-af76-e9b1432fee21",
   "metadata": {},
   "outputs": [],
   "source": [
    "view(image)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "48b151f6-cfc4-4522-aa02-04c8301d3667",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(type(image))\n",
    "\n",
    "image_arr = itk.array_view_from_image(image)\n",
    "print(type(image_arr))\n",
    "\n",
    "image_java = ij.py.to_java(image_arr)\n",
    "print(type(image_java))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b4db443d-f430-484b-a738-11b493944d8e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Invoke the Frangi vesselness op.\n",
    "vessels = np.zeros(image_arr.shape, dtype=np.float32)\n",
    "ij.op().filter().frangiVesselness(ij.py.to_java(vessels),\n",
    "                                  image_java,\n",
    "                                  [1, 1],\n",
    "                                  20)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1afc55a1-35d3-475d-83fa-9ec04fb6fa28",
   "metadata": {},
   "outputs": [],
   "source": [
    "view(vessels)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cbc436a7-5928-4017-b5d4-07f3ca21f3a0",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
